<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #div1 {
            border: 1px solid #fc54fc;
            position: absolute;
            left: 10px;
            transition: 2s all;
        }

        .h1 {
            color: #fc54fc;
            position: relative;
            width: 500px;
        }

        /* 在标签内部之前加上伪元素 */
        .h1:before {
            position: absolute;
            left: 0;
            width: 0%;
            height: 42px;
            border-bottom: 2px solid #fc54fc;
            content: "";
            display: inline-block;
            transition: 2s width;
        }

        .h1:hover:before {
            width: 100%;
        }
    </style>
</head>
<body>
    <button onclick="doClick()">点击我</button>
    <div id="div1">
        我是div1
    </div>
    <h1 class="h1">我是标题，请把鼠标浮动在我身上</h1>
    <script>

        let flag = true;

        function doClick() {
            let div = document.querySelector("#div1");
            if (flag) {
                div.style.left = "150px";
            } else {
                div.style.left = "10px";
            }
            flag = !flag;
        }
    </script>
</body>
</html>